package com.weilizhang.work1.contorl;


import com.weilizhang.work1.api.ApiResponse;
import com.weilizhang.work1.mojo.Students;
import com.weilizhang.work1.service.StudentsService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class StudentsContorl{
    @Autowired
    private StudentsService studentsService;
    @GetMapping("/list")
    public ApiResponse<List<Students>> list(Model model){
        List<Students> students = studentsService.list();

        return  ApiResponse.success(students);
    }


    // 根据学生号查询所有的某个学生的信息
    @GetMapping("/list/{studentId}")
    public  ApiResponse<Students> select(@PathVariable  Integer studentId){
        Students students = studentsService.select(studentId);
        return  ApiResponse.success(students);
    }

    @GetMapping("/majors")
    public List<String> getMajors() {
        return Arrays.asList(
                "计算机科学与技术",
                "软件工程",
                "人工智能",
                "信息安全",
                "智能科学与技术",
                "网络工程",
                "电子工程",
                "自动化"
        );
    }


    // 根据学生学号删除某个学生信息
    @DeleteMapping("/delete/{studentId}")
    public String delete(@PathVariable Integer studentId){
        System.out.println("删除学生学号为："+studentId+"的信息");
        studentsService.delete(studentId);
        return "删除成功";
    }


    // 根据学生的学号改变学生信息
    @PutMapping("/update")
    public  ApiResponse<String> update(@RequestBody Students students){
        studentsService.update(students);
        System.out.println("修改学生学号为："+students.getStudentId()+"的信息");
        return  ApiResponse.success("修改成功");
    }


    @PostMapping("/add")
    public  ApiResponse<String> add(@RequestBody Students students){
        studentsService.add(students);
        System.out.println("添加学生学号为："+students.getStudentId()+"的信息");
        return  ApiResponse.success("添加成功");
    }


    // 根据信息查询数据
    @PostMapping("/search")
    public  ApiResponse<List<Students>> search(@RequestBody Students students){
        return  ApiResponse.success(studentsService.search(students));
    }


}
